﻿//-----------------------------------------------------------------------
// <copyright file="PivotXml.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Xml
{
    using System;
    using System.Collections.ObjectModel;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    /// <summary>
    /// Represents a Pivot Collection CXML document. Contains top-level properties and elements which 
    /// pertain to the entire set of items contained within the collection.
    /// </summary>
    /// <remarks>
    /// At a high-level, CXML can be thought of as a set of property/value pairings. Facets are like 
    /// property values on an item, and facet categories are groups of facets. For example: if a collection 
    /// had a facet category called "U.S. State," then "Georgia" could be a facet in that category. 
    /// Depending on authoring choices, these facets may be displayed as filters in the Pivot collection 
    /// experience, or included in the details of an item. 
    /// </remarks>
    [Serializable()]
    [XmlRootAttribute(Namespace = "http://schemas.microsoft.com/collection/metadata/2009", IsNullable = false, ElementName = "Collection")]
    public class PivotXml : XmlNodeBase
    {
        /// <summary>
        /// Used to store the list of item.
        /// </summary>
        private Collection<ItemList> itemListCollection;

        /// <summary>
        /// Collection used to store facet category objects.
        /// </summary>
        private Collection<FacetCategory> facetCategories;

        /// <summary>
        /// Initializes a new instance of the <see cref="PivotXml"/> class.
        /// </summary>
        public PivotXml()
        {
            this.itemListCollection = new Collection<ItemList>();
            this.SchemaVersion = 1.0m;
            this.facetCategories = new Collection<FacetCategory>();
        }

        /// <summary>
        /// Gets the facet categories.
        /// </summary>
        /// <value>The facet categories of the pivot collection.</value>
        public Collection<FacetCategory> FacetCategories
        {
            get
            {
                return this.facetCategories;
            }
        }

        /// <summary>
        /// Gets the item lists.
        /// </summary>
        /// <value>The item lists of the pivot collection.</value>
        [XmlElement("Items")]
        public Collection<ItemList> ItemList
        {
            get
            {
                return this.itemListCollection;
            }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name of the pivot collection.</value>
        [XmlAttribute()]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the schema version.
        /// </summary>
        /// <value>The schema version of the pivot collection.</value>
        [XmlAttribute()]
        public decimal SchemaVersion { get; set; }

        /// <summary>
        /// Creates a pivot collectio from XML.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>A new <see cref="PivotXml"/> from the XML file specified.</returns>
        public static PivotXml CreateFromXml(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PivotXml));

            PivotXml xml = null;
            var reader = XmlTextReader.Create(fileName);

            xml = (PivotXml)serializer.Deserialize(reader);
            reader.Close();

            return xml;
        }

        /// <summary>
        /// Adds the category.
        /// </summary>
        /// <param name="category">The category.</param>
        public void AddCategory(FacetCategory category)
        {
            this.FacetCategories.Add(category);
        }

        /// <summary>
        /// Adds the item list.
        /// </summary>
        /// <param name="imageBase">The image base.</param>
        /// <returns>An index of the of the Item List.</returns>
        public int AddItemList(string imageBase)
        {
            var newItemList = new ItemList() { ImageBase = imageBase };
            this.itemListCollection.Add(newItemList);
            return this.itemListCollection.Count - 1;
        }

        /// <summary>
        /// Writes the collection to an XML file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        public void WriteXml(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PivotXml));
            
            var ns = new XmlSerializerNamespaces();
            ns.Add("p", "http://schemas.microsoft.com/livelabs/pivot/collection/2009");

            var settings = new XmlWriterSettings();
            settings.Indent = true;

            var writer = new PivotXmlWriter(fileName, Encoding.ASCII);
            writer.Formatting = Formatting.Indented;
            serializer.Serialize(writer, this, ns);
            writer.Close();
        }
    }
}
